using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace demo
{
    public class DoctorDeptSelectForm : Form
    {
        private ComboBox cboDept;
        private Button btnOk;
        private Button btnCancel;
        private Label lblTitle;

        private class ComboItem
        {
            public string Text { get; set; }
            public PatientDataService.DoctorOutpDeptPermissionItem Value { get; set; }
        }

        public PatientDataService.DoctorOutpDeptPermissionItem SelectedItem { get; private set; }

        public DoctorDeptSelectForm(List<PatientDataService.DoctorOutpDeptPermissionItem> items)
        {
            this.Text = "选择所在科室";
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ClientSize = new Size(420, 160);

            lblTitle = new Label
            {
                Text = "请选择您所在的门诊科室：",
                AutoSize = true,
                Location = new Point(20, 20)
            };

            cboDept = new ComboBox
            {
                DropDownStyle = ComboBoxStyle.DropDownList,
                Location = new Point(20, 50),
                Width = 370
            };

            btnOk = new Button
            {
                Text = "确定",
                Location = new Point(220, 100),
                Width = 80
            };
            btnOk.Click += (s, e) =>
            {
                var selected = cboDept.SelectedItem as ComboItem;
                if (selected == null || selected.Value == null)
                {
                    MessageBox.Show("请选择一个科室", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                SelectedItem = selected.Value;
                this.DialogResult = DialogResult.OK;
                this.Close();
            };

            btnCancel = new Button
            {
                Text = "取消",
                Location = new Point(310, 100),
                Width = 80
            };
            btnCancel.Click += (s, e) =>
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            };

            this.Controls.Add(lblTitle);
            this.Controls.Add(cboDept);
            this.Controls.Add(btnOk);
            this.Controls.Add(btnCancel);

            // 绑定数据
            var list = (items ?? new List<PatientDataService.DoctorOutpDeptPermissionItem>())
                .Select(x => new ComboItem
                {
                    Text = string.Format("{0} ({1})", x.DeptName, x.OrgCode),
                    Value = x
                })
                .ToList();
            cboDept.DataSource = list;
            cboDept.DisplayMember = "Text";
        }
    }
}